﻿<ad:DockableContent x:Class="ReflectionStudio.Components.UserControls.TemplateExplorer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ad="clr-namespace:AvalonDock;assembly=AvalonDock"
             xmlns:controls="clr-namespace:ReflectionStudio.Controls;assembly=ReflectionStudio.Controls"
             xmlns:converters="clr-namespace:ReflectionStudio.Components.Converters"
             xmlns:cmd="clr-namespace:ReflectionStudio.Classes"
             xmlns:local="clr-namespace:ReflectionStudio.Components.UserControls"
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
             Icon="/ReflectionStudio;component/Resources/Images/16x16/template.png"
             Title="Templates" IsCloseable="true">

    <ad:DockableContent.Resources>

        <DataTemplate x:Key="DisplayModeTemplate">
            <TextBlock Text="DisplayModeTemplate" />
        </DataTemplate>

        <DataTemplate x:Key="EditModeTemplate">
            <TextBox Text="EditModeTemplate" />
        </DataTemplate>

        <Style x:Key="{x:Type controls:TreeViewItemExtended}" TargetType="{x:Type controls:TreeViewItemExtended}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Foreground" Value="{DynamicResource NormalForegroundBrush}"/>
            <Setter Property="FocusVisualStyle" Value="{DynamicResource TreeViewExtendedItemFocusVisual}"/>
            <!--<Setter Property="HeaderTemplate" Value="{StaticResource DisplayModeTemplate}" />-->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type controls:TreeViewItemExtended}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition MinWidth="19" Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <ToggleButton x:Name="Expander" Style="{DynamicResource TreeViewExtendedItemExpandCollapseToggleStyle}"
                                IsChecked="{Binding Path=IsExpanded,
                                RelativeSource={RelativeSource TemplatedParent}}"
                                ClickMode="Press"/>
                            <Border Name="Bd"
                                  Grid.Column="1"
                                  Background="{TemplateBinding Background}"
                                  BorderBrush="{TemplateBinding BorderBrush}"
                                  BorderThickness="{TemplateBinding BorderThickness}"
                                  Padding="{TemplateBinding Padding}">
                                <StackPanel Orientation="Horizontal">
                                    <Image x:Name="PART_Image" VerticalAlignment="Center" Width="16" Height="16" Margin="0,0,5,0"
                                           Source="{TemplateBinding Tag, Converter={x:Static converters:FileInfoToImageConverter.Instance},
                                                                        ConverterParameter='Closed'}" />
                                    <ContentPresenter x:Name="PART_Header"
                                            ContentSource="Header" 
                                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                                </StackPanel>
                            </Border>
                            <ItemsPresenter x:Name="ItemsHost"
                                  Grid.Row="1"
                                  Grid.Column="1"
                                  Grid.ColumnSpan="2"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <!--<Trigger Property="IsInEditMode" Value="true">
                                <Setter Property="HeaderTemplate" Value="{StaticResource EditModeTemplate}"/>
                            </Trigger>
                            <Trigger Property="IsInEditMode" Value="false">
                                <Setter Property="HeaderTemplate" Value="{StaticResource DisplayModeTemplate}"/>
                            </Trigger>-->

                            <Trigger Property="IsExpanded" Value="false">
                                <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="IsExpanded" Value="true">
                                <Setter TargetName="PART_Image" Property="Source"
                                        Value="{Binding  Path=Tag, RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static converters:FileInfoToImageConverter.Instance},
                                                                        ConverterParameter='Open'}"/>
                            </Trigger>
                            <Trigger Property="HasItems" Value="false">
                                <Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="HasHeader" Value="false"/>
                                    <Condition Property="Width" Value="Auto"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="HasHeader" Value="false"/>
                                    <Condition Property="Height" Value="Auto"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
                            </MultiTrigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="Bd" Property="Background" Value="{DynamicResource SelectedBackgroundBrush}"/>
                                <Setter Property="Foreground" Value="{DynamicResource SelectedForegroundBrush}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="true"/>
                                    <Condition Property="IsSelectionActive" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Bd" Property="Background" Value="{DynamicResource SelectedBackgroundBrush}"/>
                                <Setter Property="Foreground" Value="{DynamicResource SelectedForegroundBrush}"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
                            </Trigger>
                            
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ad:DockableContent.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="28" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal">

            <controls:FlatImageButton Command="New"
                                      Image="/ReflectionStudio;component/Resources/Images/16x16/template.png"
                                      ToolTip="New template"/>
            <controls:FlatImageButton Command="Refresh"
                                      Image="/ReflectionStudio;component/Resources/Images/16x16/application/refresh.png"
                                      ToolTip="Refresh"/>
            <controls:FlatImageButton Command="Delete"
                                      CommandParameter="{Binding Tag}"
                                      DataContext="{Binding ElementName=treeViewTemplate, Path=SelectedItem}"
                                      Image="/ReflectionStudio;component/Resources/Images/16x16/application/delete.png"
                                      ToolTip="Delete the selected element"/>

        </StackPanel>

        <controls:TreeViewExtended Name="treeViewTemplate" Grid.Row="2" Tag="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.Tag}" PopulateOnDemand="True" SelectedItemChanged="treeViewTemplate_SelectedItemChanged" MouseDoubleClick="treeViewTemplate_MouseDoubleClick">
            <TreeView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Open"
                              Command="Open"
                              CommandParameter="{Binding Tag}"
                              DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                              IsEnabled="{Binding Tag.IsFile}">
                        <MenuItem.Icon>
                            <controls:AutoGreyableImage Source="/ReflectionStudio;component/Resources/Images/16x16/open.png" Width="16"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <Separator></Separator>
                    <MenuItem Header="Add">
                        <MenuItem Header="New template"
                                  Command="New"
                                  CommandParameter=".rst"
                                  DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                  IsEnabled="{Binding Tag.IsFolder}"/>
                        <MenuItem Header="Existing template"
                                  
                                  CommandParameter=".rst"
                                  DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                  IsEnabled="{Binding Tag.IsFolder}"/>
                        <MenuItem Header="New folder"
                                  DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                  IsEnabled="{Binding Tag.IsFolder}">
                            <MenuItem.Icon>
                                <controls:AutoGreyableImage Source="/ReflectionStudio;component/Resources/Images/16x16/folders/new_folder.png"  Width="16"/>
                            </MenuItem.Icon>
                        </MenuItem>
                    </MenuItem>
                    <Separator></Separator>
                    <MenuItem Header="Delete"
                              Command="Delete"
                              CommandParameter="{Binding Tag}"
                              DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                              IsEnabled="{Binding Tag.IsFile}">
                        <MenuItem.Icon>
                            <controls:AutoGreyableImage Source="/ReflectionStudio;component/Resources/Images/16x16/application/delete.png"  Width="16" />
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>
            </TreeView.ContextMenu>
        </controls:TreeViewExtended>
        
    </Grid>
    
</ad:DockableContent>
